# coding: utf-8
from decimal import Decimal

import os

from common.constant import StatusEnum

EMPTY = -1    # 未选择


class SpecUnit(object):
    """
    产品规格单位
    """
    # FIXME by ld_wu 2017-08-09 暂时采用拼音
    GE = 1        # 个
    TAI = 2       # 台
    GEN = 3       # 根
    TIAO = 4      # 条
    TAO = 5       # 套
    METER = 6     # 米
    SQ_M = 7      # 平方米
    W = 8         # 瓦特
    MM = 9        # 毫米
    PIXEL = 10    # 点
    NIL = 11       # 无
    DOT = 12
    SAO = 13
    C = 14
    W_METER = 15
    HZ = 16
    DOT_METER = 17
    H = 18
    DEGREE = 19
    CD_METER = 20
    G = 21
    GB = 22
    V = 23
    ZHANG = 24
    KG = 25
    U = 26
    MB = 27
    LEVEL = 28
    R_MIN = 29
    YUAN = 30
    INCH = 31
    P = 32
    A = 33
    SQ_MM = 34

    mapping = {
        GE: '个',
        TAI: '台',
        GEN: '根',
        TIAO: '条',
        TAO: '套',
        METER: 'm',
        SQ_M: '㎡',
        W: 'W',
        MM: 'mm',
        # PIXEL: 、'px',
        NIL: '无',
        DOT: 'dot',
        SAO: '扫',
        C: '℃',
        W_METER: 'W/㎡',
        HZ: 'HZ',
        DOT_METER: 'dot/㎡',
        H: 'h',
        DEGREE: '°',
        CD_METER: 'cd/㎡',
        G: 'g',
        GB: 'GB',
        V: 'V',
        ZHANG: '张',
        KG: 'KG',
        U: 'U',
        MB: 'MB',
        LEVEL: '级',
        R_MIN: 'r/min',
        YUAN: '元',
        INCH: '寸',
        P: '匹',
        A: 'A',
        SQ_MM: 'mm²'
    }

    @property
    def display_mapping(self):
        return {
            key: value for key, value in self.mapping.items() if key != self.NIL
        }

    choices = [(EMPTY, '--请选择产品规格--')] + list(mapping.items())


class SceneType(object):
    """
    户内外
    """

    OUTDOOR = 1
    INDOOR = 2
    HALF = 3

    mapping = {
        OUTDOOR: '户外',
        INDOOR: '户内',
        HALF: '半户外'
    }

    choices = [(EMPTY, '--请选择户内外--')] + list(mapping.items())


class ColorType(object):
    """
    颜色
    """

    SINGLE = 1
    DOUBLE = 2
    FULL = 3

    mapping = {
        SINGLE: '单色',
        DOUBLE: '双色',
        FULL: '全彩'
    }

    choices = [(EMPTY, '--请选择颜色--')] + list(mapping.items())


class Waterproof(object):
    """
    防水
    """

    YES = 1
    NO = 2

    mapping = {
        YES: '防水',
        NO: '非防水'
    }

    choices = [(EMPTY, '--请选择产品防水--')] + list(mapping.items())


class Transport(object):
    """
    信号传输方式
    """

    SYNC = 1
    ASYNC = 2
    DOOR_ASYNC = 3
    CURRENCY = 4

    mapping = {
        SYNC: '同步',
        ASYNC: '异步',
        DOOR_ASYNC: '门头异步',
        CURRENCY: '同异步'
    }

    choices = [(EMPTY, '--请选择信号传输方式--')] + list(mapping.items())


class AssembleWay(object):
    ORIGINAL = 1
    RENTAL = 2
    COMMON = 3
    SECTION = 4

    mapping = {
        ORIGINAL: '原装箱体',
        RENTAL: '租赁箱体',
        COMMON: '简易箱体',
        SECTION: '型材'
    }


class CalculateItem(object):
    NIL = 0
    WIDE = 1
    HIGH = 2
    W_RESOLUTION = 3
    H_RESOLUTION = 4
    POWER_RATING = 5
    W_MAX_POINTS = 6
    H_MAX_POINTS = 7
    W_PIXEL = 8
    PIXEL = 9
    W_MAX_PIXEL = 10
    H_MAX_PIXEL = 11
    T_PIXEL = 12
    MAX_LOAD_RATE = 13
    # 用于过滤
    SPACING = 14
    BOX_W_COUNT = 15
    BOX_H_COUNT = 16
    P = 17
    COLOR = 18
    SCENE = 19
    TRANSPORT = 20

    mapping = {
        NIL: '无',
        WIDE: '宽',
        HIGH: '高',
        W_RESOLUTION: '宽边分辨率',
        H_RESOLUTION: '高边分辨率',
        POWER_RATING: '额定功率',
        W_MAX_POINTS: '宽边最大点数',
        H_MAX_POINTS: '高边最大点数',
        W_PIXEL: '宽边带载点数',
        PIXEL: '总带载点数',
        W_MAX_PIXEL: '宽边最大带载点数',
        H_MAX_PIXEL: '高边最大带载点数',
        T_PIXEL: '带载点数',
        MAX_LOAD_RATE: '最大负载率',
        SPACING: '像素间距',
        BOX_W_COUNT: '箱体宽边模组数',
        BOX_H_COUNT: '箱体高边模组数',
        P: '匹',
        COLOR: '颜色',
        SCENE: '使用环境',
        TRANSPORT: '信号传输方式'
    }


scene_to_waterproof = {
    SceneType.OUTDOOR: Waterproof.YES,
    SceneType.INDOOR: Waterproof.NO,
    SceneType.HALF: Waterproof.YES
}


class PriceState(object):
    """
    个人报价的审核状态
    """
    UNCHECKED = 0
    VALID = 1
    INVALID = 2

    mapping = {
        UNCHECKED: '待审核',
        VALID: '有效',
        INVALID: '无效'
    }


class PriceRecommend(object):
    """
    个人报价是否成为系统推荐价格
    """
    NO = 0
    YES = 1

    mapping = {
        NO: '不推荐',
        YES: '推荐',
    }

# 型材长度固定为6米
SECTION_LENGTH = 6000
# 冗余率
REDUNDANCY = Decimal('0.6')
# 10的负6次方
MM_TO_M = Decimal('0.000001')
# 单条3米网线最大传输带载点数
SINGLE_RETICLE_PIXEL = 650000


class InstallWay(object):
    """
    安装方式
    """
    GROUND = 1
    WALL = 2
    SINGLE = 3
    DOUBLE = 4
    EMBED = 5
    # HANG = 6
    RENTAL = 7

    mapping = {
        GROUND: '落地式',
        WALL: '墙挂式',
        SINGLE: '单立柱',
        DOUBLE: '双立柱',
        # EMBED: '嵌入式',
        # HANG: '悬挂式',
        # RENTAL: '租赁式'
    }

# 像素间距
SPACING_LIST = [
    '0.7', '0.95', '1.27', '1.46', '1.56', '1.59', '1.9', '2.38',
    '2.5', '2.54', '3', '4', '4.75', '5', '6', '7.625', '8', '10', '16'
]

# 总带载点数300w（用于判定选择视频处理器还是拼接器）
TOTAL_PIXEL = 3000000

STEEL_INDOOR = [
    (10, 450), (50, 450), (100, 400), (150, 350), (None, 300)
]

# 钢结构梯度价格
STEEL_PRICE = {
    SceneType.OUTDOOR: {
        # 户外单立柱
        InstallWay.SINGLE: [
            (10, 1600), (50, 1300), (100, 1250), (150, 1200), (None, 1150)
        ],
        # 户外双立柱
        InstallWay.DOUBLE: [
            (10, 1700), (50, 1400), (100, 1350), (150, 1300), (None, 1250)
        ],
        # 户外落地
        InstallWay.GROUND: [
            (10, 1100), (50, 800), (100, 750), (150, 700), (None, 650)
        ],
        # 户外墙挂
        InstallWay.WALL: [
            (10, 1000), (50, 700), (100, 650), (150, 600), (None, 500)
        ],
        # 户外嵌入
        InstallWay.EMBED: [
            (10, 1000), (50, 700), (100, 650), (150, 600), (None, 500)
        ],
        # # 户外悬挂
        # InstallWay.HANG: [(None, 2000)],
        # 户外租赁
        InstallWay.RENTAL: [(None, 200)]
    },
    SceneType.INDOOR: {
        # 室内单立柱
        InstallWay.SINGLE: STEEL_INDOOR,
        # 室内双立柱
        InstallWay.DOUBLE: STEEL_INDOOR,
        # 室内落地
        InstallWay.GROUND: STEEL_INDOOR,
        # 室内墙挂
        InstallWay.WALL: STEEL_INDOOR,
        # 室内嵌入
        InstallWay.EMBED: STEEL_INDOOR,
        # # 室内悬挂
        # InstallWay.HANG: [(None, 2000)],
        # 室内租赁
        InstallWay.RENTAL: [(None, 200)]
    }
}

# 高、宽边增量（毫米）
INCREMENT = 600


class TypeToName(object):

    MODULE_GROUP = 1
    ORIGINAL_BOX = 2
    RENTAL_BOX = 3
    COMMON_BOX = 4
    BOX_FASTENER = 5
    POWER_SUPPLY = 6
    RECEIVE_CARD = 7
    ASYNC_RECEIVE_CARD = 8
    SENDING_CARD = 9
    ASYNC_SENDING_CARD = 10
    VIDEO_PROCESSOR = 11
    VIDEO_SPLICER = 12
    ASYNC_CONTROLLER = 13
    NETWORK_CARD = 14
    SCREW = 15
    STEEL_STRUCTURE = 16
    STEEL_STRUCTURE_HIGH = 17
    STEEL_STRUCTURE_WELDING = 18
    STEEL_STRUCTURE_TO_ALUMINUM = 19
    POWER_LINE = 21
    CABLE = 22
    THREE_M_RETICLE = 23
    ONE_AND_HALF_M_RETICLE = 24
    COMPUTER = 25
    EXHAUST_FAN = 26
    AIR_CONDITIONER = 27
    POWER_AMPLIFIER = 28
    SOUND_COLUMN = 29
    LIGHTNING_ROD = 30
    SECTION_BAR = 31
    SECTION_BAR_CORNER = 32
    SECTION_BAR_INTERFACE = 33
    PENDANT = 34
    SQUARE_PIPE = 35
    SPECIAL_SQUARE_PIPE = 36
    MAGNET = 37
    SECURITY = 38
    DISTRIBUTOR = 39

    mapping = {
        MODULE_GROUP: '模组',
        ORIGINAL_BOX: '原装箱体',
        RENTAL_BOX: '租赁箱体',
        COMMON_BOX: '简易箱体',
        BOX_FASTENER: '箱体扣件',
        POWER_SUPPLY: '电源',
        RECEIVE_CARD: '接收卡',
        # ASYNC_RECEIVE_CARD: '异步接收卡',
        SENDING_CARD: '发送卡',
        ASYNC_SENDING_CARD: '异步发送卡',
        VIDEO_PROCESSOR: '视频处理器',
        VIDEO_SPLICER: '视频拼接器',
        ASYNC_CONTROLLER: '异步控制器',
        NETWORK_CARD: '网卡',
        SCREW: '螺丝',
        STEEL_STRUCTURE: '钢结构',
        STEEL_STRUCTURE_HIGH: '钢结构离地高度加价',
        STEEL_STRUCTURE_WELDING: '钢结构加焊不锈钢',
        STEEL_STRUCTURE_TO_ALUMINUM: '钢结构换成铝单板',
        POWER_LINE: '电源线',
        CABLE: '排线',
        # THREE_M_RETICLE: '3米网线',
        ONE_AND_HALF_M_RETICLE: '箱体间网线',
        COMPUTER: '电脑',
        EXHAUST_FAN: '轴流风机',
        AIR_CONDITIONER: '空调',
        POWER_AMPLIFIER: '功放',
        SOUND_COLUMN: '音柱',
        LIGHTNING_ROD: '避雷针',
        SECTION_BAR: '型材',
        SECTION_BAR_CORNER: '型材拐角',
        SECTION_BAR_INTERFACE: '型材接口',
        PENDANT: '挂件',
        SQUARE_PIPE: '方管',
        SPECIAL_SQUARE_PIPE: '特殊方管',
        MAGNET: '磁铁',
        SECURITY: '安防拼接器',
        # DISTRIBUTOR: '配电柜'
    }

    # 是否需要品牌型号（True为需要，False为不需要）
    has_model = {
        MODULE_GROUP: False,
        ORIGINAL_BOX: False,
        RENTAL_BOX: False,
        COMMON_BOX: False,
        BOX_FASTENER: True,
        POWER_SUPPLY: True,
        RECEIVE_CARD: True,
        # ASYNC_RECEIVE_CARD: '异步接收卡',
        SENDING_CARD: True,
        ASYNC_SENDING_CARD: True,
        VIDEO_PROCESSOR: True,
        VIDEO_SPLICER: True,
        ASYNC_CONTROLLER: True,
        NETWORK_CARD: True,
        SCREW: True,
        STEEL_STRUCTURE: True,
        # STEEL_STRUCTURE_HIGH: '钢结构离地高度加价',
        # STEEL_STRUCTURE_WELDING: '钢结构加焊不锈钢',
        # STEEL_STRUCTURE_TO_ALUMINUM: '钢结构换成铝单板',
        POWER_LINE: True,
        CABLE: True,
        # THREE_M_RETICLE: '3米网线',
        ONE_AND_HALF_M_RETICLE: True,
        COMPUTER: True,
        EXHAUST_FAN: True,
        AIR_CONDITIONER: True,
        POWER_AMPLIFIER: True,
        SOUND_COLUMN: True,
        LIGHTNING_ROD: True,
        SECTION_BAR: True,
        SECTION_BAR_CORNER: True,
        SECTION_BAR_INTERFACE: True,
        PENDANT: True,
        SQUARE_PIPE: True,
        SPECIAL_SQUARE_PIPE: True,
        MAGNET: True,
        SECURITY: True,
        # DISTRIBUTOR: '配电柜'
    }

    to_attr = {
        MODULE_GROUP: 'module_group',
        ORIGINAL_BOX: 'box',
        RENTAL_BOX: 'box',
        COMMON_BOX: 'box',
        BOX_FASTENER: 'box_fastener',
        POWER_SUPPLY: 'power_supply',
        RECEIVE_CARD: 'receive_card',
        SENDING_CARD: 'sending_card'
    }

    to_group = {
        MODULE_GROUP: 'screen_products',
        ORIGINAL_BOX: 'assemble_products',
        RENTAL_BOX: 'assemble_products',
        COMMON_BOX: 'assemble_products',
        BOX_FASTENER: 'assemble_products',
        POWER_SUPPLY: 'screen_products',
        RECEIVE_CARD: 'screen_products',
        ASYNC_RECEIVE_CARD: 'screen_products',
        SENDING_CARD: 'screen_products',
        ASYNC_SENDING_CARD: 'screen_products',
        VIDEO_PROCESSOR: 'screen_products',
        VIDEO_SPLICER: 'screen_products',
        ASYNC_CONTROLLER: 'screen_products',
        # NETWORK_CARD: '网卡',
        SCREW: 'assemble_products',
        STEEL_STRUCTURE: 'steel_products',
        STEEL_STRUCTURE_HIGH: 'steel_products',
        STEEL_STRUCTURE_WELDING: 'steel_products',
        STEEL_STRUCTURE_TO_ALUMINUM: 'steel_products',
        POWER_LINE: 'line_products',
        CABLE: 'line_products',
        THREE_M_RETICLE: 'line_products',
        ONE_AND_HALF_M_RETICLE: 'line_products',
        COMPUTER: 'peripheral_products',
        EXHAUST_FAN: 'peripheral_products',
        AIR_CONDITIONER: 'peripheral_products',
        POWER_AMPLIFIER: 'peripheral_products',
        SOUND_COLUMN: 'peripheral_products',
        LIGHTNING_ROD: 'peripheral_products',
        SECTION_BAR: 'assemble_products',
        SECTION_BAR_CORNER: 'assemble_products',
        SECTION_BAR_INTERFACE: 'assemble_products',
        PENDANT: 'assemble_products',
        SQUARE_PIPE: 'assemble_products',
        SPECIAL_SQUARE_PIPE: 'assemble_products',
        MAGNET: 'assemble_products',
        SECURITY: 'screen_products',
        DISTRIBUTOR: 'peripheral_products'
    }


POWER_MAX_LOAD_RATE = Decimal('0.6')


class WorkingCondition(object):
    """
    create by ld_wu 2017-09-15
    工况
    """
    SECURITY = 1
    AD = 2
    DOOR = 3
    TRAFFIC = 4
    STAGE = 5

    mapping = {
        SECURITY: '安防监控',
        AD: '商用广告',
        DOOR: '门头广告',
        TRAFFIC: '交通诱导',
        STAGE: '舞台租赁'
    }


class ParamValueType(object):
    """
    create by ld_wu 2017-12-27
    技术参数的参数类型
    """
    INT = 1
    STRING = 2
    LIST = 3

    mapping = {
        INT: '整型',
        STRING: '字符串',
        LIST: '列表'
    }


CONSULT_DISPLAY = '议价'


class TagType(object):
    """
    create by ld_wu 2018-01-11
    技术参数标签类型
    """
    INPUT = 1
    SINGLE = 2
    MULTIPLE = 3
    BLANK = 4

    mapping = {
        INPUT: '输入项',
        SINGLE: '单选项',
        MULTIPLE: '多选项',
        BLANK: '跳转选择项'
    }


class VisibleType(object):
    """
    create by ld_wu 2018-01-17
    价格可见类型
    """
    FRIEND = 1
    SELF = 11
    ALL = 21
    PROV = 31

    mapping = {
        FRIEND: '好友可见',
        SELF: '仅自己可见',
        ALL: '全国可见',
        PROV: '部分可见'
    }


class DraftStatus(StatusEnum):
    """
    create by ld_wu 2018-01-02
    产品草稿审核状态
    """
    HANDING = (1, '审核中')
    REFUSE = (11, '拒绝')
    PASS = (21, '通过')
    CLOSE = (51, '审核结束')


if __name__ == '__main__':
    import json

    f = open(
        os.path.join(os.path.dirname(__file__), 'product_constants.json'),
        'w',
        encoding='utf-8'
    )

    product_types = [
        dict(
            id_=key,
            name=value,
            has_model=TypeToName.has_model.get(key, False)
        )
        for key, value in TypeToName.mapping.items()
        if key not in
           [
               TypeToName.STEEL_STRUCTURE_HIGH,
               TypeToName.STEEL_STRUCTURE_WELDING,
               TypeToName.STEEL_STRUCTURE_TO_ALUMINUM,
               TypeToName.BOX_FASTENER,
               TypeToName.SPECIAL_SQUARE_PIPE
           ]
    ]

    units = [
        dict(
            id_=key,
            name=value
        )
        for key, value in SpecUnit.mapping.items()
    ]

    calculate_items = [
        dict(
            id_=key,
            name=value
        )
        for key, value in CalculateItem.mapping.items()
    ]

    f.write(
        json.dumps(
            dict(
                version='1.0',
                product_types=product_types,
                units=units,
                calculate_items=calculate_items
            ),
            ensure_ascii=False
        ),
    )
    f.close()
